#include "glfile.h"
#include <QCoreApplication>
#include <QDir>
#include "easytool.h"

GLFile::GLFile()
{

}

void GLFile::creat(QString path)
{

    file.setFileName(path + "/video.gl");
    file.open(QIODevice::WriteOnly);

    audiofile.setFileName(path + "/audio.wav");
    audiofile.open(QIODevice::ReadOnly);
    audiofile.seek(100);

    QDir dir(path + "/image/");
    dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    dir.setSorting(QDir::Time|QDir::Reversed);

    QFileInfoList list = dir.entryInfoList();

    audio_offset = 1024;
    audio_size = audiofile.size() - 100;
    video_offset = audio_offset + audio_size;
    video_size = list.size() * 1024;

    writeHead();
    writeAudio();

    for (int i = 0; i < list.size(); i++) {
//        QFileInfo fileInfo = list.at(i);
//        qDebug() << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10)
//                                                        .arg(fileInfo.fileName()));
        getImageData(path + "/image/" + list.at(i).fileName());
    }

    file.close();
}

void GLFile::writeHead(void)
{
    uint8_t head[1024],count = 0;
    head[count++] = (uint8_t)(audio_offset>>24);
    head[count++] = (uint8_t)(audio_offset>>16);
    head[count++] = (uint8_t)(audio_offset>>8);
    head[count++] = (uint8_t)(audio_offset>>0);

    head[count++] = (uint8_t)(audio_size>>24);
    head[count++] = (uint8_t)(audio_size>>16);
    head[count++] = (uint8_t)(audio_size>>8);
    head[count++] = (uint8_t)(audio_size>>0);

    head[count++] = (uint8_t)(video_offset>>24);
    head[count++] = (uint8_t)(video_offset>>16);
    head[count++] = (uint8_t)(video_offset>>8);
    head[count++] = (uint8_t)(video_offset>>0);

    head[count++] = (uint8_t)(video_size>>24);
    head[count++] = (uint8_t)(video_size>>16);
    head[count++] = (uint8_t)(video_size>>8);
    head[count++] = (uint8_t)(video_size>>0);

    file.write((const char *)head,1024);
    file.flush();
}

void GLFile::writeAudio(void)
{
    file.write(audiofile.readAll());
    file.flush();
    audiofile.close();
}

void GLFile::getImageData(QString imgpath)
{
    unsigned char imgdata[128*64],imgb[128*64/8];
    unsigned char count = 0,data = 0;
    unsigned int countb_data = 0,count_data = 0;

    //qDebug() << imgpath;

    QImage image(imgpath);
    for(int i =0;i<image.width();i++)
    {
         for(int j=0;j<image.height();j++)
         {
            QRgb pixel = image.pixel(i,j);
            if(qRed(pixel) > 128)
            {
                data |= 0x80;
#ifdef  DEBUG_IMG
                imgdata[count_data++] = 0xFF;
#endif
            }else{
                data |= 0x00;
#ifdef  DEBUG_IMG
                imgdata[count_data++] = 0x00;
#endif
            }
            count++;
            if(count >= 8)
            {
                imgb[countb_data++] = data;
                count = 0;
                data = 0;
            }else{
                data = data >> 1;
            }
         }
    }

    file.write((const char *)imgb,countb_data);
    file.flush();

#ifdef  DEBUG_IMG
    QImage img = QImage(imgdata,128,64,QImage::Format_Grayscale8);
    img.save(imgpath+".png","png");
#endif
}
